package com.lbtc.controller;

import com.github.pagehelper.PageInfo;
import com.lbtc.common.ApiRestResponse;
import com.lbtc.common.Constant;
import com.lbtc.exception.LbtcMallException;
import com.lbtc.exception.LbtcMallExceptionEnum;
import com.lbtc.request.AddProductReq;
import com.lbtc.request.UpdateProductReq;
import com.lbtc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.UUID;

@RestController
@RequestMapping
public class ProductAdminController {

    @Autowired
    private ProductService productService;

    @PostMapping("/admin/product/add")
    public ApiRestResponse addProduct(@RequestBody AddProductReq addProductReq) throws LbtcMallException {
        productService.add(addProductReq);
        return ApiRestResponse.success();
    }


    @Value("${files.upload.dir}")
    private String fileUploadDir;//D:/IdeaProjects/LBtc/files/

    @PostMapping("/admin/upload/file")
    public ApiRestResponse upload(HttpServletRequest httpServletRequest, @RequestParam MultipartFile file) throws LbtcMallException {
        //修改前台上传文件格式
        String originalFilename = file.getOriginalFilename();//1.png
        String suffixType = originalFilename.substring(originalFilename.lastIndexOf("."));//.png
        UUID uuid = UUID.randomUUID();
        String newFileName = uuid.toString() + suffixType;//c5f00389-5e9a-4094-b02d-36a2ca45b3d5.png
        File filePath = new File(Constant.FILE_UPLOAD_DIR);//D:\IdeaProjects\LBtc\files
        File destFile = new File(Constant.FILE_UPLOAD_DIR + newFileName);
        /*File filePath = new File(fileUploadDir);//D:\IdeaProjects\LBtc\files
        File destFile = new File(fileUploadDir + newFileName);*/
        //在磁盘上创建路径
        if (!filePath.exists()) {
            if (!filePath.mkdir()) {
                throw new LbtcMallException(LbtcMallExceptionEnum.CREATE_FAILED);
            }
        }
        //上传到磁盘
        try {
            file.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            return ApiRestResponse.
                    success(getHost(new URI(httpServletRequest.getRequestURI() + "")) + "/upload/" + newFileName);
            //success(getHost(new URL(httpServletRequest.getRequestURL() + "")) + "/upload/" + newFileName);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return ApiRestResponse.error(1, "上传失败");

        }
    }

    private URI getHost(URI uri) {
        URI effectiveURI;
        try {
            effectiveURI = new
                    URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), null, null, null);
        } catch (URISyntaxException e) {
            effectiveURI = null;
        }
        return effectiveURI;
    }

    @PostMapping("/admin/product/update")
    public ApiRestResponse updateProduct(@Valid @RequestBody UpdateProductReq updateProductReq) throws LbtcMallException {
        productService.update(updateProductReq);
        return ApiRestResponse.success();
    }

    @PostMapping("/admin/product/delete")
    public ApiRestResponse deleteProduct(@RequestParam Integer id) throws LbtcMallException {
        productService.delete(id);
        return ApiRestResponse.success();
    }
    @PostMapping("/admin/product/batchUpdateSellStatus")
    public ApiRestResponse batchUpdateSellStatus(@RequestParam Integer[] ids,@RequestParam Integer
            sellStatus) {
        productService.batchUpdateSellStatus(ids, sellStatus);
        return ApiRestResponse.success();
    }
    @GetMapping("/admin/product/list")
    public ApiRestResponse list(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        PageInfo pageInfo = productService.listForAdmin(pageNum, pageSize);
        return ApiRestResponse.success(pageInfo);
    }
}
